<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\CommonLogic;
use App\Model\OACompany;
use App\Model\OACurrency;
use App\Model\OACustomer;
use App\Model\OADept;
use Illuminate\Http\Request;

/**
 * Class CommonController
 * @package App\Http\Controllers\OA
 * @desc 通用控制器
 */
class CommonController extends Controller
{
    private $input_type = [
        'dept',//部门
        'company',//公司
        'customer',//客户
        'currency',//货币
        'position',//职位
        'staff',//员工
        ];
    public function getInput(Request $request){
        try {
            $rules = [
                'type'=>'required|array',
            ];
            $this->verify($request->all(),$rules);
            $type_list = $request->input('type');
            $deptid = $request->input('deptid');
            foreach ($type_list as $item){
                if(!in_array($item,$this->input_type)) throw new \Exception('查找的数据类型不存在');
            }
            $logic = new CommonLogic();
            $data = $logic->getCommonData($type_list,$deptid);
            ret_json('ok', 'ok', $data);
        }catch(\Exception $e){
            ret_json('err',$e->getMessage());
        }

    }

    //获取职位列表
    public function getPositionList(Request $request)
    {
        $rules = [
            'type'=>'present|nullable|string'
        ];
        $this->verify($request->all(),$rules);
        try{
            $logic = new CommonLogic();
            $list = $logic->getCommonData($request->all());
            ret_json('ok','ok',['list'=>$list]);
        }catch(\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //通过职位获取用户
    public function getUserListByPositionId(Request $request)
    {
        $rules = ['position_id'=>'required|exists:position,id'];

        $this->verify($request->all(),$rules);

        try{
            $logic = new CommonLogic();
            $list = $logic->getUserListByPositionId($request->all());
            ret_json('ok','ok',['list'=>$list]);
        }catch(\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取所有职位用户
    public function getAllRoleAndUser(Request $request)
    {
        $logic = new CommonLogic();
        $list = $logic->getAllRoleAndUser($request);
        ret_json('ok','ok',['list'=>$list]);
    }

    //获取公司部门
    public function getCompanyDept(Request $request)
    {
        $logic = new CommonLogic();
        $list = $logic->getCompanyInfo($request->all());
        ret_json('ok','ok',['list'=>$list]);
    }
}
